package com.micro.ai.agents.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.agents.entity.AgentConfig;
import com.micro.ai.agents.service.AgentConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 智能体配置控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/agent-configs")
@Tag(name = "智能体配置管理", description = "智能体配置的增删改查功能")
public class AgentConfigController {

    @Autowired
    private AgentConfigService agentConfigService;

    @PostMapping
    @Operation(summary = "创建配置", description = "创建新的智能体配置")
    public ApiResponse<AgentConfig> create(@Valid @RequestBody AgentConfig config) {
        AgentConfig result = agentConfigService.create(config);
        return ApiResponse.success("创建成功", result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新配置", description = "更新智能体配置")
    public ApiResponse<AgentConfig> update(
            @Parameter(description = "配置ID", required = true) @PathVariable String id,
            @Valid @RequestBody AgentConfig config) {
        AgentConfig result = agentConfigService.update(id, config);
        return ApiResponse.success("更新成功", result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除配置", description = "删除智能体配置")
    public ApiResponse<Void> delete(
            @Parameter(description = "配置ID", required = true) @PathVariable String id) {
        agentConfigService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/by-agent/{agentId}")
    @Operation(summary = "查询智能体配置列表", description = "根据智能体ID查询所有配置")
    public ApiResponse<List<AgentConfig>> listByAgentId(
            @Parameter(description = "智能体ID", required = true) @PathVariable String agentId) {
        List<AgentConfig> list = agentConfigService.listByAgentId(agentId);
        return ApiResponse.success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询配置", description = "支持多条件查询配置列表")
    public ApiResponse<Page<AgentConfig>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "租户ID") @RequestParam(value = "tenantId", required = false) String tenantId,
            @Parameter(description = "智能体ID") @RequestParam(value = "agentId", required = false) String agentId) {
        Page<AgentConfig> page = agentConfigService.pageQuery(pageNum, pageSize, tenantId, agentId);
        return ApiResponse.success(page);
    }
}

